/*
 * SearchLyricPanel.java
 *
 * Created on 2007年12月25日, 下午7:51
 */
package com.hadeslee.yoyoplayer.setting;

import com.hadeslee.yoyoplayer.util.Config;
import com.hadeslee.yoyoplayer.util.FileNameFilter;
import com.hadeslee.yoyoplayer.util.Util;
import java.io.File;
import java.util.Vector;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author  hadeslee
 */
public class SearchLyricPanel extends javax.swing.JPanel implements Initable{
    private static Logger log=Logger.getLogger(SearchLyricPanel.class.getName());
    private Vector<File> files;
    private File lyricDir;

    /** Creates new form SearchLyricPanel */
    public SearchLyricPanel() {
        initComponents();
        init();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        list = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        autoSearch = new javax.swing.JCheckBox();
        searchWhenFull = new javax.swing.JCheckBox();
        selectBest = new javax.swing.JCheckBox();
        relativeWithFile = new javax.swing.JCheckBox();
        autoOverWrite = new javax.swing.JCheckBox();
        saveAsTheFileName = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        saveLyricDir = new javax.swing.JTextField();
        jButton5 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(Config.getResource("SearchLyricPanel.localSearch"))); // NOI18N

        list.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "USER.HOME", "YOYO.HOME" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(list);

        jButton1.setText(Config.getResource("SearchLyricPanel.add")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText(Config.getResource("SearchLyricPanel.delete")); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText(Config.getResource("SearchLyricPanel.up")); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText(Config.getResource("SearchLyricPanel.down")); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton4)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE))
                .addContainerGap())
        );

        autoSearch.setText(Config.getResource("autoSearchLyric")); // NOI18N

        searchWhenFull.setText(Config.getResource("SearchLyricPanel.searchOnlyWhenTagFull")); // NOI18N

        selectBest.setText(Config.getResource("SearchLyricPanel.selectBest")); // NOI18N

        relativeWithFile.setText(Config.getResource("SearchLyricPanel.autoRelative")); // NOI18N

        autoOverWrite.setText(Config.getResource("SearchLyricPanel.autoOverwrite")); // NOI18N

        saveAsTheFileName.setText(Config.getResource("SearchLyricPanel.saveAsTheSameNameAsSongFile")); // NOI18N

        jLabel1.setText(Config.getResource("SearchLyricPanel.saveFileTo")); // NOI18N

        saveLyricDir.setEditable(false);

        jButton5.setText(Config.getResource("SearchLyricPanel.change")); // NOI18N
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton9.setText(Config.getResource("save")); // NOI18N
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton9);

        jButton10.setText(Config.getResource("reset")); // NOI18N
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton10);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(autoSearch)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                                .addComponent(searchWhenFull))
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(autoOverWrite)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                                .addComponent(saveAsTheFileName))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(selectBest)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                                .addComponent(relativeWithFile)))
                        .addGap(18, 18, 18))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(saveLyricDir, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton5)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 415, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(autoSearch)
                    .addComponent(searchWhenFull))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(selectBest)
                    .addComponent(relativeWithFile))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(autoOverWrite)
                    .addComponent(saveAsTheFileName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(saveLyricDir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        JFileChooser jfc = Util.getFileChooser(new FileNameFilter("*", Config.getResource("SearchLyricPanel.folder")), JFileChooser.DIRECTORIES_ONLY);
        if (jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            File dir = jfc.getSelectedFile();
            if (!files.contains(dir)) {
                files.add(dir);
                list.setListData(files);
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        File value = (File) list.getSelectedValue();
        if (value != null) {
            files.remove(value);
            list.setListData(files);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        File obj = (File) list.getSelectedValue();
        if (obj != null) {
            int index = files.indexOf(obj);
            if (index > 0) {
                files.remove(obj);
                files.add(index - 1, obj);
            }
            list.setListData(files);
            list.setSelectedValue(obj, true);
        }
       
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        File obj = (File) list.getSelectedValue();
        if (obj != null) {
            int index = files.indexOf(obj);
            if (index < files.size() - 1) {
                files.remove(obj);
                files.add(index + 1, obj);
            }
            list.setListData(files);
            list.setSelectedValue(obj, true);
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        if(Config.getConfig().isLinux()){
            int i=JOptionPane.showConfirmDialog(this, Config.getResource("SearchLyricPanel.warning1") +
                    Config.getResource("SearchLyricPanel.warning2"),Config.getResource("SearchLyricPanel.confirm"),JOptionPane.YES_NO_OPTION);
            if(i!=JOptionPane.YES_OPTION){
                return ;
            }
        }
        JFileChooser jfc = Util.getFileChooser(new FileNameFilter("*", Config.getResource("SearchLyricPanel.folder")), JFileChooser.DIRECTORIES_ONLY);
        if (jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            lyricDir = jfc.getSelectedFile();
            saveLyricDir.setText(lyricDir.getPath());
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        // TODO add your handling code here:
        doSave();
    }//GEN-LAST:event_jButton9ActionPerformed

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
        // TODO add your handling code here:
        init();
    }//GEN-LAST:event_jButton10ActionPerformed

    private void doSave() {
        Config config = Config.getConfig();
        config.setSearchLyricDirs(files);
        config.setAutoSearchLyricOnline(autoSearch.isSelected());
        config.setSearchWhenInfoFull(searchWhenFull.isSelected());
        config.setSelectBestLyric(selectBest.isSelected());
        config.setAutoRelatingWithMediaFile(relativeWithFile.isSelected());
        config.setAutoOverWriteExistFile(autoOverWrite.isSelected());
        config.setSaveTheSameNameAsMediaFile(saveAsTheFileName.isSelected());
        config.setSaveLyricDir(lyricDir);
        //自动把这个目录加到搜索目录去
        if(!files.contains(lyricDir)){
            files.add(lyricDir);
        }
        if (config.isAutoCloseDialogWhenSave()) {
            config.getOptionDialog().setVisible(false);
        }
    }

    public  void init() {
        Config config = Config.getConfig();
        files = new Vector<File>();
        files.addAll(config.getSearchLyricDirs());
        list.setListData(files);
        autoSearch.setSelected(config.isAutoSearchLyricOnline());
        searchWhenFull.setSelected(config.isSearchWhenInfoFull());
        selectBest.setSelected(config.isSelectBestLyric());
        relativeWithFile.setSelected(config.isAutoRelatingWithMediaFile());
        autoOverWrite.setSelected(config.isAutoOverWriteExistFile());
        saveAsTheFileName.setSelected(config.isSaveTheSameNameAsMediaFile());
        lyricDir = config.getSaveLyricDir();
        if (lyricDir != null) {
            saveLyricDir.setText(lyricDir.getPath());
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox autoOverWrite;
    private javax.swing.JCheckBox autoSearch;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList list;
    private javax.swing.JCheckBox relativeWithFile;
    private javax.swing.JCheckBox saveAsTheFileName;
    private javax.swing.JTextField saveLyricDir;
    private javax.swing.JCheckBox searchWhenFull;
    private javax.swing.JCheckBox selectBest;
    // End of variables declaration//GEN-END:variables
}
